{{template "header.html" .}}</head><body>
{{template "nav.html" (dict "navtab" "task" "authUser" .authUser )}}

<div class="container-fluid">
<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item">任务管理</li>
        <li class="breadcrumb-item active" aria-current="page">#{{.task.Id}} <span class="task-title">{{.task.Title}}</span></li>
    </ol>
</nav>
<hr />
</div>

<div class="container-fluid" id="related-tasks">
<!-- 关联任务 -->
{{if gt (len .tasks) 1}}
{{template "task-related.html" .}}
{{end}}
</div>

<div class="container-fluid">

<div class="card mt-3 mb-3">
    <div class="card-header d-flex align-items-center">
        <div><b>#{{.task.Id}} <span class="task-title">{{.task.Title}}</span></b></div>
        <div class="ms-auto">
            <button
                class="btn btn-link "
                onclick="_modify_task()"
            >
            <i class="fa-solid fa-pen-to-square"></i>
                编辑
            </button>

            <a
                class="btn btn-link ms-2"
                href="/task/new/{{.task.Id}}"
                data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-content="复制内容，创建子任务"
            >
                <i class="fa-solid fa-clone"></i>
                复制
            </a>


            <button
                class="btn btn-link ms-2"
                onclick="$('#dialog-addrelated').modal('show');"
            >
            <i class="fa-solid fa-magnet"></i>
                关联
            </button>

            <a
                class="btn btn-link ms-2"
                href="#new-comment-anchor"
            >
                <i class="fa-solid fa-message"></i> 回复
            </a>

            <button
                class="btn btn-link ms-2"
                onclick="ajax('/task/log/{{.task.Id}}')"
            >
                <i class="fa-solid fa-calendar-days"></i>日志
            </button>

        </div>
    </div>

    <div class="card-body" id="task-modify-form">
<p><i>
    <span class="me-5">作者：{{(index $.users .task.Author).Name}} ({{.task.CreatedAt | formatDate}} <span class="text-primary">{{.task.CreatedAt | SecondsToTime}}</span> )
    </span><span id="task-last-modify">修改：{{(index $.users .task.Editor).Name}} ({{.task.UpdatedAt | formatDate}})
    </span>
</i></p>
<hr />

<!-- 快速修改-->
<div>
    <div class="row row-cols-lg-auto g-3 align-items-center">

<div class="col-12"><div class="input-group">
  <span class="input-group-text">标题</span>
  <input type="text" class="form-control" value="{{.task.Title}}" name="title" style="width: 800px;" />
</div></div>

<div class="col-12"><div class="input-group">
    <div class="input-group-text">时间</div>
    <input value="{{.task.StartAt | unixToDay}}" type="date" class="form-control" name="start_at" />
    <input value="{{.task.EndAt | unixToDay}}" type="date" class="form-control" name="end_at" />
</div></div>

    </div>

    <div class="row row-cols-lg-auto g-3 align-items-center mt-2">
        <div class="col-12">

<div class="input-group">
    <select itag="val" name="project"
        class="form-select"
        value="{{.task.Project}}"
        onchange="onChangePro( this.value, '#filterTags' );"
    >
{{template "selection_map.html" (dict "values" .projects "selected" .task.Project) }}
    </select>
    <select id="filterTags" itag="val" name="tag"
        class="form-select"
        value="{{.task.Tag}}"
    >
{{template "selection_tag.html" (dict "values" .tags "selected" .task.Tag "filterId" .task.Project) }}
    </select>
</div>

        </div>

        <div class="col-12">

<div class="input-group">
    <div class="input-group-text">执行</div>
    <select itag="val" name="leader_dept"
        class="form-select"
        value="{{.task.LeaderDept}}"
        onchange="onChangeDepartment( this.value, '#filterLeader' );"
    >
{{template "selection_map.html" (dict "values" .props.department "selected" .task.LeaderDept) }}
    </select>
    <select id="filterLeader" itag="val" name="leader"
        class="form-select"
        value="{{.task.Leader}}"
    >
{{template "selection_user.html" (dict "values" .users "selected" .task.Leader "filterId" .task.LeaderDept) }}
    </select>
</div>

        </div>

        <div class="col-12">

<div class="input-group">
    <div class="input-group-text">验收</div>
    <select itag="val" name="checker_dept"
        class="form-select"
        value="{{.task.CheckerDept}}"
        onchange="onChangeDepartment( this.value, '#filterChecker' );"
    >
{{template "selection_map.html" (dict "values" .props.department "selected" .task.CheckerDept) }}
    </select>
    <select id="filterChecker" itag="val" name="checker"
        class="form-select"
        value="{{.task.Checker}}"
    >
{{template "selection_user.html" (dict "values" .users "selected" .task.Checker "filterId" .task.CheckerDept) }}
    </select>
</div>

        </div>

<div class="col-12"><div class="input-group">
    <div class="input-group-text">品控</div>
    <select itag="val" name="tester_dept"
        class="form-select"
        value="{{.task.TesterDept}}"
        onchange="onChangeDepartment( this.value, '#filterTester' );"
    >
{{template "selection_map.html" (dict "values" .props.department "selected" .task.TesterDept) }}
    </select>
    <select id="filterTester" itag="val" name="tester"
        class="form-select"
        value="{{.task.Tester}}"
    >
{{template "selection_user.html" (dict "values" .users "selected" .task.Tester "filterId" .task.TesterDept) }}
    </select>

</div></div>


<div class="col-12"><div class="input-group">

<select  itag="val" name="caty"
    class="form-select"
    value="{{.task.Caty}}"
>
{{template "selection_map.html" (dict "values" .props.caty "selected" .task.Caty) }}
</select>

</div></div>

<div class="col-12"><div class="input-group">

<select itag="val" name="status"
    class="form-select"
    value="{{.task.Status}}"
>
{{template "selection_map.html" (dict "values" .props.status "selected" .task.Status) }}
</select>

</div></div>

<div class="col-12"><div class="input-group">
<select itag="val" name="priority"
    class="form-select"
    value="{{.task.Priority}}"
>
{{template "selection_map.html" (dict "values" .props.priority "selected" .task.Priority) }}
</select>
<input type="text" class="form-control" itag="val" name="level" value="{{.task.Level}}" />

</div></div>

<div class="col-12">
<button class="btn btn-danger" onclick="_fast_modify()">
    <i class="fa-solid fa-floppy-disk"></i> 一键保存
</button>
</div>



    </div>

    <div class="edit-buttons mt-3" style="display: none;">
<button class="btn btn-danger" onclick="_fast_modify()">
    <i class="fa-solid fa-floppy-disk"></i> 保存
</button>
<button class="btn btn-secondary ms-2" onclick="_modify_task()">
    <i class="fa-solid fa-ban"></i> 取消
</button>
    </div>
</div>
<!-- 快速修改 -->


        <hr />
<div id="task-content" style="min-height: 150px;">{{.task.Content|html}}</div>



    </div><!-- card-body -->

<div class="card-footer edit-buttons" style="display: none;">
<button class="btn btn-danger" onclick="_fast_modify()">
    <i class="fa-solid fa-floppy-disk"></i> 保存
</button>
<button class="btn btn-secondary ms-2" onclick="_modify_task()">
    <i class="fa-solid fa-ban"></i> 取消
</button>
</div>

</div>


<!-- 反馈 -->
<div id="comments">
{{range $index, $comment := .comments}}
    {{template "comment-one.html" (dict "index" $index "comment" $comment "users" $.users)}}
{{end}}
</div>
<!-- 反馈 end -->


<!-- 添加反馈 -->
<hr id="new-comment-anchor">

<blockquote><p class="text-primary">在下面提交完成情况、测试反馈、其他意见......</p></blockquote>
<form id="new-comment-form" method="POST" action="/comment/save" onsubmit="return false;">
<input type="hidden" name="task_id" value="{{.task.Id}}">

<textarea id="new-comment-editor" name="content"></textarea>
<div class="mt-3"></div>
<div class="d-grid">
<button type="button" class="btn btn-danger btn-lg" onclick="_commit_new_comment('#new-comment-form', '#new-comment-editor')"> 提交反馈 </button>
</div>


</form>
<!-- 添加反馈 end -->

    <div class="mt-3"></div>
</div>

<div class="modal" id="dialog-addrelated" tabindex="-1" aria-hidden="true">

<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title">关联到</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body" id="related-to-form">
            <p>输入要关联到的ID。可以是其他的任务ID。如果对方是子任务，将自动关联到其主任务。</p>
            <p>此行为会将这个单子进行重新关联。原有的关联也会取消。</p>
            <p>输入0表示不做关联。</p>

            <input type="hidden" name="task_id" value="{{.task.Id}}">
            <input name="related_id" class="form-control form-control-lg" value="0" type="number" />

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            <button onclick="_related_to()" type="button" class="btn btn-primary">提交</button>
        </div>
    </div>
</div>

</div>


{{template "footer.html" .}}

<script type="text/javascript">
var users = {{.users | json}};
var tags = {{.tags | json}};
var task = {{.task | json}};

$(document).ready(function() {
    initEditor("#new-comment-editor");
});


function _related_to() {
    var data = getFormData("#related-to-form");
    ajax("/task/relatedto", data, function(r) {
        $('#dialog-addrelated').modal('hide');
        _set_related();
    })
}

function _commit_new_comment( formid, editorid ) {
    var c = $(editorid).summernote( 'code' );

    if (c.indexOf("data:image/png;base64") >= 0) {
        alert('不正确的图片格式，不要从word、有道等软件中直接粘贴过来，建议使用ctl+shift+v');
        return false;
    }

    if (c.indexOf('yne-bulb-block') >= 0) {
        alert('不要从word、有道等软件中直接粘贴过来，使用ctl+shift+v')
        return false;
    }

    submitFormAjax(formid, {
        success: function(r) {
            if (!handleResponse(r)) {
                return;
            }

            $('#comments').append(r.content);
            $(editorid).summernote('reset');
            $('#dialog').modal('hide');
        }
    });

    return true;
}

function _set_related() {
    ajax("/task/related/{{.task.Id}}")
}

var _status_modify_task = false
function _modify_task() {
    if (_status_modify_task) {
        $('#task-content').summernote('destroy');
    } else {
        initEditor( '#task-content', true, 300 )
    }
    $('.edit-buttons').toggle();
    _status_modify_task = !_status_modify_task;
}

function _fast_modify() {
    var jsonData = getFormData("#task-modify-form");
    jsonData.start_at = new Date(jsonData.start_at).getTime() / 1000;
    jsonData.end_at = new Date(jsonData.end_at).getTime() / 1000;

    if (_status_modify_task) {
        var markup = $('#task-content').summernote('code');
        jsonData.content = markup;
    }


    //对比旧数据，如果无变化则不提交，对比所有字段
    for (var k in task) {
        //判断jsonData中是否有k字段
        if (!jsonData.hasOwnProperty(k)) {
            continue;
        }
        if (jsonData[k] == task[k]) {
            delete jsonData[k];
        }
    }
    if (Object.keys(jsonData).length == 0) {
        return showErrorToast('没有修改任何数据')
    }

    jsonData.id = task.id;
    jsonData.lockn = task.lockn + 1;

    ajax("/task/save", jsonData, function(r) {
        showSuccessToast('保存成功')
        if (_status_modify_task) {
            _modify_task()//关闭编辑
        }

        //新值覆盖旧值
        for (var k in jsonData) {
            task[k] = jsonData[k];
        }

        $(".task-title").html(jsonData.title);
        let iname = $("#iname").html();
        let date = new Date();
        $('#task-last-modify').html(`修改：${iname} (${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()})`)
    })
}


var _modify_comment_id = 0
function _modify_comment(id) {
    let isnew = false
    if (_modify_comment_id > 0) {
        //关闭旧的编辑
        $('#comment-content-' + _modify_comment_id).summernote('destroy');
        $('#edit-comment-buttons-' + _modify_comment_id).toggle();

        if (id != _modify_comment_id) {
            isnew = true
        }
    } else {
        isnew = true
    }

    if (isnew) {
        _modify_comment_id = id
        initEditor( '#comment-content-' + id, true, 200 )
        $('#edit-comment-buttons-' + id).toggle();
    } else {
        _modify_comment_id = 0
    }
}

function _commit_comment(id) {
    var c = $('#comment-content-' + id).summernote( 'code' );

    if (c.indexOf("data:image/png;base64") >= 0) {
        alert('不正确的图片格式，不要从word、有道等软件中直接粘贴过来，建议使用ctl+shift+v');
        return ;
    }

    if (c.indexOf('yne-bulb-block') >= 0) {
        alert('不要从word、有道等软件中直接粘贴过来，使用ctl+shift+v')
        return ;
    }

    ajax("/comment/save", {
        id : id,
        content : c
    }, function(r) {
        showSuccessToast('保存成功')

        _modify_comment(id)//关闭编辑

        let iname = $("#iname").html();
        let date = new Date();
        $('#comment-last-modify-'+id).html(`修改：${iname} (${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()})`)
    })
}

</script>


</body></html>
